package com.mengxuegu.springboot.Controller;

import com.mengxuegu.springboot.entities.Provider;
import com.mengxuegu.springboot.mapper.ProviderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class ProviderController {
    Logger logger = LoggerFactory.getLogger(getClass());

//    @Autowired
//    ProviderDao providerDao;
    @Autowired
    ProviderMapper providerMapper;

    /**
     * 查询list
     * @param map
     * @return
     */
    @GetMapping("/providers")
    public String list( Map<String,Object> map ,  Provider provider){
//        Collection<Provider> all = providerDao.getAll(  );

        List<Provider> providers = providerMapper.getProviders(provider);
        map.put("providerName",  provider.getProviderName());
        map.put("providers",providers);
        return "provider/list";
    }

    @GetMapping("/provider/{pid}")
    public String view(@PathVariable("pid") int pid , @RequestParam(value = "type" ,defaultValue = "view") String type ,Map<String,Object> map){
        logger.info("查询"+pid+"订单详情");
//        Provider provider = providerDao.getProvider(pid);
        Provider providerByPid = providerMapper.getProviderByPid(pid);
        map.put("provider",providerByPid);
        return "provider/"+type;
    }

    //修改供应商信息
    @PutMapping("/provider")
    public String update(Provider provider) {
        logger.info("更改供应商信息。。。");
        //更新操作

        int i = providerMapper.updateProvider(provider);
        return "redirect:providers";
    }


    //去保存页面
    @GetMapping("/provider")
    public String toadd(){
        return "provider/add";
    }

    //保存provider
    @PostMapping("/provider")
    public String save(Provider provider){

        int i = providerMapper.addProvider(provider);

        return "redirect:/providers";
    }

    //删除provider
    @DeleteMapping("/provider/{pid}")
    public  String delete(@PathVariable("pid")  Integer pid){
        providerMapper.deleteProviderByPid(pid);
        return "redirect:/providers";
    }


}
